<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 *
 *
 *
 * @package     ceo
 * @subpackage  models
 * @category    models
 * @author      ydd team, wu
 * @copyright   copyright (c) 2012, yaoduoduo, Inc.
 * @filesource
 */
class Client_model extends Base_model {

    protected $_table_name = 'client';
    protected $_key_name = 'client_id';

    function get_client_info($id) {
        if (!empty($id))
            $where[$this->_key_name] = $id;
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'row');
    }

    function get_many($name , $mobile) {
        
        $this->db->like("name",$name);
        $this->db->like("mobile",$mobile);
        $qe=$this->db->get($this->_table_name);
        return $qe->row();
        
        
    }
	
	
	function insert($data) {

		$this->db->insert('h_client',$data);

	}
	
    function get_client2($id) {

        $this->db->where($this->_key_name, $id);
        $query = $this->db->get($this->_table_name);
        return $query;
    }

    function update_relationship($data, $id) {        
        $this->db->where('client_relationship_id', $id);
        $this->db->update('h_client_relationship', $data);
    }

    function get_client_by_name($name) {

        $this->db->where('name', $name);
        $query = $this->db->get($this->_table_name);
        return $query;
    }

    function get_client_by_verify_code($code) {

        $this->db->where('ref_client_verify_code', $code);
        $query = $this->db->get($this->_table_name);
        return $query;
    }

    //增加聯絡人
    function insert_contact($data, $verify_code) {

        if (!empty($data) AND !empty($verify_code)) {
            $c_data = array("name" => $data['name'], "english_name" => $data['english_name'], "sex" => $data['sex'], "mobile" => $data['mobile'], "home_phone" => $data['home_phone'], "company_phone" => $data['company_phone'], "fax" => $data['fax'], "home_address" => $data['home_address'], "company_address" => $data['company_address'], "ref_client_verify_code" => $verify_code);
            $this->db->insert('h_client_contact', $c_data);
        }
    }

    //增加聯絡人
    function add_contact($data) {

        if (!empty($data)) {

            $this->db->insert('h_client_contact', $data);
        }
    }

    //增加聯絡人
    function demand_insert_contact($data) {

        $this->db->insert('h_client_contact', $data);
    }

    //增加關係人
    function insert_relationship($data, $verify_code) {

        $c_data = array("name" => $data['name'],
            "english_name" => $data['english_name'],
            "sex" => $data['sex'],
            "mobile" => $data['mobile'],
            "home_phone" => $data['home_phone'],
            "company_phone" => $data['company_phone'],
            "fax" => $data['fax'],
            "home_address" => $data['home_address'],
            "company_address" => $data['company_address'],
            "ref_client_verify_code" => $verify_code
        );

        $this->db->insert('h_client_relationship', $c_data);
    }

    //增加關係人
    function insert_relationship_2($data, $id) {

        //$c_data = $data;
        //$c_data["ref_client_verify_code"] = $verify_code;   
        //$this->db->where('client_relationship_id', $id);         
        $this->db->insert('h_client_relationship', $data);
    }

    function update_contact($data, $id) {

        $this->db->where('client_contact_id', $id);
        $this->db->update('h_client_contact', $data);
    }

    function update_contact_submit($data, $id) {

        $this->db->where('client_relationship_id', $id);
        $this->db->update('h_client_relationship', $data);
    }

    function do_client_excel($ids) {

        if (!empty($ids)) {

            foreach ($ids as $num => $id) {
                $this->db->or_where($this->_key_name, $id);
            }
            $res = $this->db->get("client");
        } else {

            $res = $this->db->get("client");
        }
        return $res;
    }

    //匯出excel by client 全部
    //個人的就匯出個人的客人
    function do_client_excel_all() {

        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();
        
        if ($purview!='#all') {

            $this->db->where('front_man', $users_id);
            $this->db->where('broker', $users_id);
        }



        $this->db->order_by("ctime", "desc"); 
        $res = $this->db->get("client");        
        return $res;
    }

    function get_client_name($name) {
        if (!empty($name))
            $where["name"] = $name;
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'row');
    }

    function get_client_name2($name) {

        if (!empty($name)) {

            $this->db->where('name', $name);
            $result = $this->db->get($this->_table_name);

            return $result;
        }
    }

    function insert_client_history($data, $id) {

        $query = $this->db->get_where('h_client_history', array('client_id' => $id));

        //echo 'kakai';
        //$data['shop_point_id'] = $this->login_lib->shop_point_id();
        $data['ctime'] = time();
        $data['client_id'] = $id;
        $data['version'] = $query->num_rows() + 1;

        $this->db->insert('h_client_history', $data);
    }

    function get_client_name_count($name) {
        if ($name != "")
            $where["name"] = $name;
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, 'count');
    }

    function get_client_email_by_id() {
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        $c = array();
        foreach ($result as $key => $val) {
            $e = "";
            if ($val->email != "" && $val->send_email == "Y")
                $e = $val->email;
            if ($val->email1 != "" && $val->send_email1 == "Y") {
                if ($e != "")
                    $e .= "," . $val->email1;
                else
                    $e .= $val->email1;
            }
            if ($val->email2 != "" && $val->send_email2 == "Y") {
                if ($e != "")
                    $e .= "," . $val->email2;
                else
                    $e .= $val->email2;
            }
            $c[$val->client_id] = $e;
        }
        return $c;
    }

    function get_client_mobile_by_id() {
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        $c = array();
        foreach ($result as $key => $val) {
            $m = "";
            if ($val->mobile != "" && $val->send_sms == "Y")
                $m = $val->mobile;
            if ($val->mobile1 != "" && $val->send_sms1 == "Y") {
                if ($m != "")
                    $m .= "," . $val->mobile1;
                else
                    $m .= $val->mobile1;
            }
            if ($val->mobile2 != "" && $val->send_sms2 == "Y") {
                if ($m != "")
                    $m .= "," . $val->mobile2;
                else
                    $m .= $val->mobile2;
            }
            $c[$val->client_id] = $m;
        }
        return $c;
    }

    function get_client($uri_query, $limit) {
        $this->set_limit($limit);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where = array('dtime' => 0, 'name' => $uri_query['keyword']);
        return $this->fetch(NULL, $where);
    }

    function get_client_count($uri_query) {
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $con["where"][] = array('dtime' => 0, 'name' => $uri_query['keyword']);
        $this->set_args_array($con);
        return $this->fetch_count();
    }

    function get_all() {

        //$user_purview = check_broker_is_visible();
        //if (!$user_purview) {
        //$this->set_where('front_man,broker', $this->login_lib->users_id());
        //}

        $where = array('dtime' => 0);
        return $this->fetch(NULL, $where);
    }


    // function get_object_recommended($keyword) {

    //     $CI = &get_instance();
    //     $purview = $CI->login_lib->purview();
    //     $users_id = $CI->login_lib->users_id();



    //     if ($keyword != "")
    //         $this->db->like('case_name', $keyword);

         
    //     //if ($purview!='#all') {
    //         //$this->db->where('front_man', $users_id);
    //         //$this->db->where('broker', $users_id);
    //     //}

    //     $this->db->where('dtime', 0);
    //     $this->db->limit(10,0);
    //     $result=$this->db->get('h_object');


    //     $p = array();
    //     foreach ($result->result() as $val) {

    //         $p[$val->object_id]["case_name"] = urlencode($val->case_name);
    //         $p[$val->object_id]["building_name"] = urlencode($val->recommended);

    //     }
    //     return $p;
    // }

    function get_client_recommended($keyword) {

        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();



        if ($keyword != "")
            $this->db->like('name', $keyword);

        //$this->db->insert('h_sql' , array("SQL"=>$users_id.' ~ '.$purview , 'type'=>'before' ));

        if ($purview!='#all') {

            //$this->db->insert('h_sql' , array("SQL"=>$users_id.' / '.$purview , 'type'=>'after' ));

            $this->db->where('front_man', $users_id);
            $this->db->where('broker', $users_id);
        }
        $this->db->where('dtime', 0);
        $result=$this->db->get($this->_table_name);

        //$result = $this->fetch(NULL, $where);


        $p = array();
        foreach ($result->result() as $val) {
            $p[$val->client_id]["name"] = urlencode($val->name);
            $p[$val->client_id]["recommended"] = urlencode($val->recommended);
        }
        return $p;
    }

    function get_demand_client($keyword) {

        if ($keyword != "")
            $this->db->like('name', $keyword, 'after');
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }

        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        $this->load->model('building_name_model');
        $building_name = $this->building_name_model->get_building_name();
        $p = array();
        foreach ($result as $key => $val) {
            $p[$val->client_id]["name"] = urlencode($val->name);
            $p[$val->client_id]["recommended"] = urlencode($val->recommended);
            $p[$val->client_id]["ref_client_verify_code"] = urlencode($val->ref_client_verify_code);
            $p[$val->client_id]["mobile"] = urlencode($val->mobile);
            $p[$val->client_id]["home_phone"] = urlencode($val->home_phone);
            $p[$val->client_id]["company_phone"] = urlencode($val->company_phone);
            $p[$val->client_id]["email"] = urlencode($val->email);
            $p[$val->client_id]["home_address"] = urlencode($val->home_address);
            if (isset($building_name[$val->home_building_name]))
                $p[$val->client_id]["home_building_name"] = urlencode($building_name[$val->home_building_name]);
            else
                $p[$val->client_id]["home_building_name"] = $val->home_building_name;
            $p[$val->client_id]["company_name"] = urlencode($val->company_name);
            $p[$val->client_id]["company_address"] = urlencode($val->company_address);
        }

        return $p;
    }

    function get_demand_client_broker_no_visible($keyword) {


        $this->db->where("front_man", $this->login_lib->users_id());
        $this->db->where("broker", $this->login_lib->users_id());
        $this->db->like("name", $keyword);
        $result = $this->db->get($this->_table_name);




        foreach ($result->result() as $val) {
            $p[$val->client_id]["client_id"] = urlencode($val->client_id);
            $p[$val->client_id]["name"] = urlencode($val->name);
            $p[$val->client_id]["recommended"] = urlencode($val->recommended);
            $p[$val->client_id]["ref_client_verify_code"] = urlencode($val->ref_client_verify_code);
            $p[$val->client_id]["mobile"] = urlencode($val->mobile);
            $p[$val->client_id]["home_phone"] = urlencode($val->home_phone);
            $p[$val->client_id]["company_phone"] = urlencode($val->company_phone);
            $p[$val->client_id]["email"] = urlencode($val->email);
            $p[$val->client_id]["home_address"] = urlencode($val->home_address);
            //if (isset($building_name[$val->home_building_name]))
            $p[$val->client_id]["home_building_name"] =  urlencode($val->home_building_name);
            //else
            //  $p[$val->client_id]["home_building_name"] = '';//$val->home_building_name;
            $p[$val->client_id]["company_name"] = urlencode($val->company_name);
            $p[$val->client_id]["company_address"] = urlencode($val->company_address);

            //if (!empty($contact->name)) {
            $p[$val->client_id]["contact_name"] = ''; //urlencode($contact->name);
            $p[$val->client_id]["contact_home_phone"] = ''; //urlencode($contact->home_phone);
            //} else {
            //  $p[$val->client_id]["contact_name"] = '';
            //  $p[$val->client_id]["contact_home_phone"] = '';
            //}
        }
        return $p;
    }

    function get_demand_client_broker_visible($keyword) {


        //$this->db->where("front_man" ,$this->login_lib->users_id() );
        //$this->db->where("broker" ,$this->login_lib->users_id() );
        $this->db->like("name", $keyword);
        $result = $this->db->get($this->_table_name);




        foreach ($result->result() as $val) {
            $p[$val->client_id]["client_id"] = urlencode($val->client_id);
            $p[$val->client_id]["name"] = urlencode($val->name);
            $p[$val->client_id]["recommended"] = urlencode($val->recommended);
            $p[$val->client_id]["ref_client_verify_code"] = urlencode($val->ref_client_verify_code);
            $p[$val->client_id]["mobile"] = urlencode($val->mobile);
            $p[$val->client_id]["home_phone"] = urlencode($val->home_phone);
            $p[$val->client_id]["company_phone"] = urlencode($val->company_phone);
            $p[$val->client_id]["email"] = urlencode($val->email);
            $p[$val->client_id]["home_address"] = urlencode($val->home_address);
            //if (isset($building_name[$val->home_building_name]))
            $p[$val->client_id]["home_building_name"] = urlencode($val->home_building_name);
            //else

            $p[$val->client_id]["company_name"] = urlencode($val->company_name);
            $p[$val->client_id]["company_address"] = urlencode($val->company_address);

            //if (!empty($contact->name)) {
            $p[$val->client_id]["contact_name"] = ''; //urlencode($contact->name);
            $p[$val->client_id]["contact_home_phone"] = ''; //urlencode($contact->home_phone);
            //} else {
            //  $p[$val->client_id]["contact_name"] = '';
            //  $p[$val->client_id]["contact_home_phone"] = '';
            //}
        }
        return $p;
    }

    function get_demand_client2($keyword) {

        if ($keyword != "")
            $this->db->like('name', $keyword, 'after');
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }

        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        $this->load->model('building_name_model');
        $building_name = $this->building_name_model->get_building_name();
        $p = array();

        $ref_client_verify_code;

        foreach ($result as $row) {
            $ref_client_verify_code = $row->ref_client_verify_code;
        }

        $this->db->where('ref_client_verify_code', $ref_client_verify_code);
        $contact = $this->db->get('h_client_contact');
        $contact = $contact->row();

        foreach ($result as $key => $val) {
            $p[$val->client_id]["client_id"] = urlencode($val->client_id);
            $p[$val->client_id]["name"] = urlencode($val->name);
            $p[$val->client_id]["recommended"] = urlencode($val->recommended);
            $p[$val->client_id]["ref_client_verify_code"] = urlencode($val->ref_client_verify_code);
            $p[$val->client_id]["mobile"] = urlencode($val->mobile);
            $p[$val->client_id]["home_phone"] = urlencode($val->home_phone);
            $p[$val->client_id]["company_phone"] = urlencode($val->company_phone);
            $p[$val->client_id]["email"] = urlencode($val->email);
            $p[$val->client_id]["home_address"] = urlencode($val->home_address);
            if (isset($building_name[$val->home_building_name]))
                $p[$val->client_id]["home_building_name"] = urlencode($building_name[$val->home_building_name]);
            else
                $p[$val->client_id]["home_building_name"] = $val->home_building_name;
            $p[$val->client_id]["company_name"] = urlencode($val->company_name);
            $p[$val->client_id]["company_address"] = urlencode($val->company_address);

            if (!empty($contact->name)) {
                $p[$val->client_id]["contact_name"] = urlencode($contact->name);
                $p[$val->client_id]["contact_home_phone"] = urlencode($contact->home_phone);
            } else {
                $p[$val->client_id]["contact_name"] = '';
                $p[$val->client_id]["contact_home_phone"] = '';
            }
        }
        return $p;
    }

    function get_client_contact($verify_code) {

        $this->db->where('ref_client_verify_code', $verify_code);
        $contacts = $this->db->get('h_client_contact');

        foreach ($contacts->result() as $contact) {
            $p[$contact->client_contact_id]["name"] = urlencode($contact->name);
            $p[$contact->client_contact_id]["home_phone"] = urlencode($contact->home_phone);
            $p[$contact->client_contact_id]["mobile"] = urlencode($contact->mobile);
            $p[$contact->client_contact_id]["email"] = urlencode($contact->email);
            $p[$contact->client_contact_id]["client_contact_id"] = urlencode($contact->client_contact_id);
        }

        return $p;
    }

    function get_object_client($keyword) {

        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();



        if ($keyword != "")
            $this->db->like('name', $keyword,'after');

        //$this->db->insert('h_sql' , array("SQL"=>$users_id.' ~ '.$purview , 'type'=>'before' ));

        if ($purview!='#all') {

            //$this->db->insert('h_sql' , array("SQL"=>$users_id.' / '.$purview , 'type'=>'after' ));

            $this->db->where('front_man', $users_id);
            $this->db->where('broker', $users_id);
        }
        $this->db->where('dtime', 0);
        $result=$this->db->get($this->_table_name);

        //$result = $this->fetch(NULL, $where);


        $p = array();
        //foreach ($result->result() as $val) {
            //$p[$val->client_id]["name"] = urlencode($val->name);
            //$p[$val->client_id]["recommended"] = urlencode($val->recommended);
        //}
        //return $p;


        /*
        if ($keyword != "")
            $this->db->like('name', $keyword, 'after');
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        $result = $this->fetch(NULL, $where);
        */
        $this->load->model('building_name_model');
        $building_name = $this->building_name_model->get_building_name();

        $p = array();
        foreach ($result->result() as  $val) {
            $p[$val->client_id]["client_id"] = urlencode($val->client_id);
            $p[$val->client_id]["name"] = urlencode($val->name);
            $p[$val->client_id]["recommended"] = urlencode($val->recommended);
            $p[$val->client_id]["mobile"] = urlencode($val->mobile);
            $p[$val->client_id]["home_phone"] = urlencode($val->home_phone);
            $p[$val->client_id]["company_phone"] = urlencode($val->company_phone);
        }
        return $p;
    }

    function get_company($keyword) {
        if ($keyword != "")
            $this->set_where('company_name,company_name1,company_name2', $keyword, 'after');
        //,company_name1,company_name2
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where = array('dtime' => 0);
        $result = $this->fetch(NULL, $where);
        //echo $this->db->last_query();
        $p = array();
        $cn = array();
        foreach ($result as $key => $val) {
            if ($val->company_name != "" && $keyword != "") {
                if (!in_array($val->company_name, $cn) && strncmp($val->company_name, $keyword, strlen($keyword)) == 0) {
                    $cn[$val->client_id] = $val->company_name;
                    $p[$val->client_id]["name"] = $val->company_name;
                    $p[$val->client_id]["company_name"] = urlencode($val->company_name);
                    $p[$val->client_id]["type_of_company"] = urlencode($val->type_of_company);
                    $p[$val->client_id]["unicode"] = urlencode($val->unicode);
                    $p[$val->client_id]["category"] = urlencode($val->category);
                    $p[$val->client_id]["company_address"] = urlencode($val->company_address);
                    $p[$val->client_id]["company_building_name"] = urlencode($val->company_building_name);
                    $p[$val->client_id]["company_send_publication"] = urlencode($val->company_send_publication);
                    $p[$val->client_id]["company_website"] = urlencode($val->company_website);
                }
            }
            if ($val->company_name1 != "" && $keyword != "") {
                if (!in_array($val->company_name1, $cn) && strncmp($val->company_name1, $keyword, strlen($keyword)) == 0) {
                    $cn[$val->client_id . "_1"] = $val->company_name1;
                    $p[$val->client_id . "_1"]["name"] = $val->company_name1;
                    $p[$val->client_id . "_1"]["company_name"] = urlencode($val->company_name1);
                    $p[$val->client_id . "_1"]["type_of_company"] = urlencode($val->type_of_company1);
                    $p[$val->client_id . "_1"]["unicode"] = urlencode($val->unicode1);
                    $p[$val->client_id . "_1"]["category"] = urlencode($val->category1);
                    $p[$val->client_id . "_1"]["company_address"] = urlencode($val->company_address1);
                    $p[$val->client_id . "_1"]["company_building_name"] = urlencode($val->company_building_name1);
                    $p[$val->client_id . "_1"]["company_send_publication"] = urlencode($val->company_send_publication1);
                    $p[$val->client_id . "_1"]["company_website"] = urlencode($val->company_website1);
                }
            }

            if ($val->company_name2 != "" && $keyword != "") {
                if (!in_array($val->company_name2, $cn) && strncmp($val->company_name2, $keyword, strlen($keyword)) == 0) {
                    $cn[$val->client_id . "_2"] = $val->company_name2;
                    $p[$val->client_id . "_2"]["company_name"] = urlencode($val->company_name2);
                    $p[$val->client_id . "_2"]["type_of_company"] = urlencode($val->type_of_company2);
                    $p[$val->client_id . "_2"]["unicode"] = urlencode($val->unicode2);
                    $p[$val->client_id . "_2"]["category"] = urlencode($val->category2);
                    $p[$val->client_id . "_2"]["company_address"] = urlencode($val->company_address2);
                    $p[$val->client_id . "_2"]["company_building_name"] = urlencode($val->company_building_name2);
                    $p[$val->client_id . "_2"]["company_send_publication"] = urlencode($val->company_send_publication2);
                    $p[$val->client_id . "_2"]["company_website"] = urlencode($val->company_website2);
                }
            }
        }
        return $p;
    }

    function query_client($w = null, $keyword = null, $limit = null, $sequence = null) {
        $this->db->order_by("ctime", "DESC");
        /*
          if($sequence=="new_insert")
          $this->db->order_by("ctime", "DESC");
          else if($sequence!=""){
          //$sq=explode("/",$sequence);
          //$this->db->order_by($sq[0], $sq[1]);
          $this->db->order_by("ctime", "DESC");
          }
         */

        $this->set_limit($limit);
        if ($keyword != "")
            $this->set_where('customer_source,background_and_features', $keyword);
        if (isset($w['name']))
            $this->set_where('name', $w['name']);
        if (isset($w['recommended']))
            $this->set_where('recommended', $w['recommended']);
        if (isset($w['importance'])) {
            $this->db->where_in('importance', $w['importance']);
        }
        if (isset($w['nationality'])) {
            $this->db->where_in('nationality', $w['nationality']);
        }
        if (isset($w['home_address']))
            $this->set_where('home_address', $w['home_address']);
        if (isset($w['home_building_name'])) {
            $this->set_where('home_building_name,home_building_name1,home_building_name2,company_building_name,company_building_name1,company_building_name2', $w['home_building_name']);
        }
        if (isset($w['type_of_company']))
            $where['type_of_company'] = $w['type_of_company'];
        if (isset($w['customer_source'])) {
            $this->db->where_in('customer_source', $w['customer_source']);
        }
        if (isset($w['company_name'])) {
            $this->db->like('company_name', $w['company_name'], 'after');
        }
        if (isset($w['mobile'])) {
            $this->set_where('mobile,mobile1,mobile2,home_phone,home_phone1,home_phone2,company_phone,company_phone1,company_phone2,company_phone_ext,company_phone_ext1,company_phone_ext2', $w['mobile']);
        }
        if (isset($w['sctime'])) {
            $ts = explode("/", $w['sctime']);
            $where['ctime >='] = mktime(0, 0, 0, $ts[1], $ts[2], $ts[0]);
        }
        if (isset($w['ectime'])) {
            $te = explode("/", $w['ectime']);
            $where['ctime <='] = mktime(23, 59, 59, $te[1], $te[2], $te[0]);
        }
        if (isset($w['sbirthday'])) {
            $t = explode("/", $w['sbirthday']);
            $where['birthday_int >='] = mktime(0, 0, 0, $t[1], $t[2], $t[0]);
        }
        if (isset($w['ebirthday'])) {
            $t = explode("/", $w['ebirthday']);
            $where['birthday_int <='] = mktime(23, 59, 59, $t[1], $t[2], $t[0]);
        }

        if (isset($w['send_publication'])) {
            $this->set_where('home_send_publication,home_send_publication1,home_send_publication2,company_send_publication,company_send_publication1,company_send_publication2', $w['send_publication']);
        }
        if (isset($w['send_email'])) {
            $this->set_where('send_email,send_email1,send_email2', $w['send_email']);
        }
        if (isset($w['gifts']))
            $where['gifts'] = $w['gifts'];
        if (isset($w['broker']))
            $this->set_where('broker', $w['broker']);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where);
    }

    function query_client_count($w = null, $keyword = null) {
        if ($keyword != "")
            $this->set_where('customer_source,background_and_features', $keyword);
        if (isset($w['name']))
            $this->set_where('name', $w['name']);
        if (isset($w['recommended']))
            $this->set_where('recommended', $w['recommended']);
        if (isset($w['importance'])) {
            $this->db->where_in('importance', $w['importance']);
        }
        if (isset($w['nationality'])) {
            $this->db->where_in('nationality', $w['nationality']);
        }
        if (isset($w['home_address']))
            $this->set_where('home_address', $w['home_address']);
        if (isset($w['home_building_name'])) {
            $this->set_where('home_building_name,home_building_name1,home_building_name2,company_building_name,company_building_name1,company_building_name2', $w['home_building_name']);
        }
        if (isset($w['type_of_company']))
            $where['type_of_company'] = $w['type_of_company'];
        if (isset($w['customer_source'])) {
            $this->db->where_in('customer_source', $w['customer_source']);
        }
        if (isset($w['company_name'])) {
            $this->db->like('company_name', $w['company_name'], 'after');
        }
        if (isset($w['mobile'])) {
            $this->set_where('mobile,mobile1,mobile2,home_phone,home_phone1,home_phone2,company_phone,company_phone1,company_phone2,company_phone_ext,company_phone_ext1,company_phone_ext2', $w['mobile']);
        }
        if (isset($w['sctime'])) {
            $ts = explode("/", $w['sctime']);
            $where['ctime >'] = mktime(0, 0, 0, $ts[1], $ts[2], $ts[0]);
        }
        if (isset($w['ectime'])) {
            $te = explode("/", $w['ectime']);
            $where['ctime <'] = mktime(23, 59, 59, $te[1], $te[2], $te[0]);
        }
        if (isset($w['sbirthday'])) {
            $t = explode("/", $w['sbirthday']);
            $where['birthday >'] = mktime(0, 0, 0, $t[1], $t[2], $t[0]);
        }
        if (isset($w['ebirthday'])) {
            $t = explode("/", $w['ebirthday']);
            $where['birthday <'] = mktime(23, 59, 59, $t[1], $t[2], $t[0]);
        }

        if (isset($w['send_publication'])) {
            $this->set_where('home_send_publication,home_send_publication1,home_send_publication2,company_send_publication,company_send_publication1,company_send_publication2', $w['send_publication']);
        }
        if (isset($w['send_email'])) {
            $this->set_where('send_email,send_email1,send_email2', $w['send_email']);
        }
        if (isset($w['gifts']))
            $where['gifts'] = $w['gifts'];
        if (isset($w['broker']))
            $this->set_where('broker', $w['broker']);
        $user_purview = check_broker_is_visible();
        if (!$user_purview) {
            $this->set_where('front_man,broker', $this->login_lib->users_id());
        }
        $where['dtime'] = 0;
        return $this->fetch(NULL, $where, NULL, "count");
    }

    function update_client($data, $id) {

        $where[] = array('client_id' => $id);
        return $this->update($data, $where);
    }

    function update_client_two($data, $id) {

        $this->db->where($this->_key_name, $id);
        $this->db->update($this->_table_name, $data);
    }

    function add_client($data) {

        $data['shop_point_id'] = $this->login_lib->shop_point_id();
        $data['ctime'] = time();

        return $this->insert($data);
    }
	
	function insert_client($data) {


        return $this->db->insert('h_client',$data);
    }
	
	
    function add_client2($data) {

        $data['shop_point_id'] = $this->login_lib->shop_point_id();
        $data['ctime'] = time();

        $this->db->insert("h_client", $data);
    }

    function del_client_crid($id) {

        $this->db->where('client_id', $id);
        $this->db->delete($this->_table_name);
    }

    function del_client($data) {
        $where[] = array('client_id' => $data['id']);
        $value = array('dtime' => time());
        return $this->update($value, $where);
    }

    function add_client_by_id($d) {
        $where['client_id'] = $d['client_id'];
        $where['dtime'] = 0;
        $data = $this->fetch(NULL, $where, NULL, 'row');
        $data['shop_point_id'] = $this->login_lib->shop_point_id();
        $data['ctime'] = time();
        $data["name"] = $d["name"];
        $data["mobile"] = $d["mobile"];
        $data["home_phone"] = $d["home_phone"];
        $data["recommended"] = $d["recommended"];
        return $this->insert($data);
    }

    function copy_client($data) {
        if ($data["client_id"] != "")
            return $this->add_client_by_id($data);
        else
            return $this->add_client($data);
    }

    function sample_clients() {

        $query = $this->db->get($this->_table_name);
        return $query;
    }

    function countRec($table_name, $query) {

        $sql = " SELECT count(*) as `count` FROM `h_client` " . $query;

        $query = $this->db->query($sql);

        $total;

        foreach ($query->result() as $row) {
            $total = $row->count;
        }

        return $total;
    }

    function client_for_crid($page, $rp, $sortname, $sortorder, $query) {

        $roder_by = " ORDER BY  `" . $sortname . "` " . $sortorder . " ";

        $start = (($page - 1) * $rp);
        $limit = "LIMIT $start, $rp";

        $sql_round = " SELECT * FROM  " . "  `h_client` " . $query . " " . $roder_by . " " . $limit;

        $total_rows = $this->countRec("h_circle", $query);

        $query = $this->db->query($sql_round);
        return $query;
    }

}
